<template>
    <div class="file_layout">
       <el-tree
            :data="data4"
            :props="defaultProps"
            show-checkbox
            node-key="id"
            default-expand-all
            :expand-on-click-node="false"
            :render-content="renderContent">
        </el-tree>
        <el-dialog
            title="新增子集"
            :visible.sync="dialogVisible"
            width="25%"
            >
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addChild">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
let id = 1000;
export default {
    name: "",
    data() {
        return {
            dialogVisible:false,
            form:{
                name:''
            },
            data4: [{
                id: 1,
                label: 'LK',
                children: [{
                    id: 3,
                    label: 'G',
                    children: [{
                    id: 6,
                    label: 'G01'
                    }, {
                    id: 7,
                    label: 'G02'
                    }]
                }]
                }, {
                id: 2,
                label: 'ZZPark',
                children: [{
                    id: 4,
                    label: 'A',
                    children: [{
                        id: 8,
                        label: 'A01'
                        }, {
                        id:9,
                        label: 'A02'
                    }]
                }, {
                    id: 5,
                    label: 'B',
                    children: [{
                        id: 10,
                        label: 'A03'
                        }, {
                        id:11,
                        label: 'A04'
                    }]
                }]
                }, ],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            tempdata:{}
        }
    },
    components: {},
    methods: {
        append(data) {
            this.dialogVisible = true;
            this.tempdata = data;
        },
        addChild(){
            let data = this.tempdata;
            let label = this.form.name;
            const newChild = { id: id++, label, children: [] };
            if (!data.children) {
            this.$set(data, 'children', []);
            }
            data.children.push(newChild);
            this.tempdata = {};
            this.dialogVisible = false;
        },
        remove(node, data) {
            const parent = node.parent;
            const children = parent.data.children || parent.data;
            const index = children.findIndex(d => d.id === data.id);
            children.splice(index, 1);
        },

        renderContent(h, { node, data, store }) {
            return (
            <span style="flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px;">
                <span>
                <span>{node.label}</span>
                </span>
                <span>
                <el-button style="font-size: 12px;" type="text" on-click={ () => this.append(data) }>新增子集</el-button>
                <el-button style="font-size: 12px;" type="text" on-click={ () => this.remove(node, data) }>删除</el-button>
                </span>
            </span>);
        }
        }
}
</script>

<style lang="scss" scoped>
.file_layout{
    width:500px;
    margin:20px auto;
    padding: 20px;
    border:1px solid #f0f0f0;
}
</style>
